﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlcUI_test.MyControls
{
    [Description("二选一按钮控件")]
    public partial class ButtonCheck : UserControl
    {
        # region 字段Field
        private bool _isButtonClicked = false;
        private string _buttonText1 = "";    // 存放按钮名称
        private string _buttonText2 = "";
        #endregion

        #region 属性Property
        /// <summary>
        /// 外面调用时，通过这个值获取是左边选中还是右边选中
        /// </summary>
        public bool IsButtonClicked
        {
            get { return _isButtonClicked; }
            private set { _isButtonClicked = value; }
        }

        [Description("为真时按钮的名称")]
        public string ButtonText1
        {
            get { return _buttonText1; }
            set 
            {   _buttonText1 = value; 
                radioButton1.Text = value; 
                
            }  
            // 这样写，当设置ButtonText1时，radioButton1的Text也会随之改变
        }
        [Description("为假时按钮的名称")]
        public string ButtonText2
        {
            get { return _buttonText2; }
            set 
            { 
                _buttonText2 = value; 
                radioButton2.Text = value; 
            }
        }

        [Description("制空按钮的名称")]
        public string ButtonText3
        {
            get { return radioButton3.Text; }
            private set
            {
                radioButton3.Text = value;
            }
        }


        public string ValueName { get; set; }

        #endregion


        public ButtonCheck()
        {
            InitializeComponent();
            radioButton3.Text = "无操作";
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                IsButtonClicked = radioButton1.Checked;
                // Console.WriteLine(radioButton1.Text + " is checked " + IsButtonClicked);
                ButtonCheckedChanged?.Invoke(this, IsButtonClicked);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                IsButtonClicked = !radioButton2.Checked;
                // Console.WriteLine(radioButton2.Text + " is checked " + IsButtonClicked);
                ButtonCheckedChanged?.Invoke(this, IsButtonClicked);  
                // this 指的就是ButtonCheck控件
                // sender 指的是radioButton2控件，来源是不一样的，后续的事件处理会反应出来问题
            }
        }

        // TODO 需要一个自定义事件控制组合控件值的改变
        /// <summary>
        /// 按钮值改变事件，返回一个bool值
        /// </summary>
        [Description("按钮值改变时触发")]
        public event EventHandler<bool> ButtonCheckedChanged;

        public void Initialize()
        {
            radioButton3.Checked = true;  // 默认右边选中
        }
    }
}
